#ifndef PIN_H__
#define PIN_H__

#include "stdint.h"
#include "ptdef.h"

#ifdef __cplusplus
extern "C" {
#endif


struct pt_device_pin
{
    struct pt_device parent;
    const struct pt_pin_ops *ops;
};
struct pt_device_pin_status
{
    uint16_t pin;
    uint16_t status;
};
struct pt_device_pin_mode
{
    uint16_t pin;
    uint16_t mode;
};
#define PIN_LOW                 0x00
#define PIN_HIGH                0x01

#define PIN_MODE_OUTPUT         0x00
#define PIN_MODE_INPUT          0x01
#define PIN_MODE_INPUT_PULLUP   0x02
#define PIN_MODE_INPUT_PULLDOWN 0x03
#define PIN_MODE_OUTPUT_OD      0x04

#define PIN_IRQ_MODE_RISING             0x00
#define PIN_IRQ_MODE_FALLING            0x01
#define PIN_IRQ_MODE_RISING_FALLING     0x02
#define PIN_IRQ_MODE_HIGH_LEVEL         0x03
#define PIN_IRQ_MODE_LOW_LEVEL          0x04

#define PIN_IRQ_DISABLE                 0x00
#define PIN_IRQ_ENABLE                  0x01

#define PIN_IRQ_PIN_NONE                -1

struct pt_pin_ops
{
    void (*pin_mode)(struct pt_device *device, int32_t pin, int32_t mode);
    void (*pin_write)(struct pt_device *device, int32_t pin, int32_t value);
    int (*pin_read)(struct pt_device *device, int32_t pin);

    /* TODO: add GPIO interrupt */
    int32_t (*pin_attach_irq)(struct pt_device *device, int32_t pin,
                      uint32_t mode, void (*hdr)(void *args), void *args);
    int32_t (*pin_detach_irq)(struct pt_device *device, int32_t pin);
    int32_t (*pin_irq_enable)(struct pt_device *device, int32_t pin, uint32_t enabled);
};

int pt_device_pin_register(uint8_t device_id, const struct pt_pin_ops *ops, void *user_data);
void pt_pin_write(int32_t pin, int32_t value);
void pt_pin_mode ( int32_t pin, int32_t mode );
int pt_pin_read ( int32_t pin );

#ifdef __cplusplus
}
#endif

#endif
